<script lang="ts">
  import Hayai from './../../../lib/Tools/Hayai.svelte';
  import UmaMusumeBirthdays from './../../../lib/Tools/UmaMusumeBirthdays.svelte';
  import ActivityHistory from '$lib/Tools/ActivityHistory/Tool.svelte';
  import Wrapped from '$lib/Tools/Wrapped/Tool.svelte';
  import EpisodeDiscussionCollector from '$lib/Tools/EpisodeDiscussionCollector.svelte';
  import CharacterBirthdays from '$lib/Tools/Birthdays.svelte';
  import SequelSpy from '$lib/Tools/SequelSpy/Tool.svelte';
  import { closest } from '$lib/Error/path';
  import HeadTitle from '$lib/Home/HeadTitle.svelte';
  import RandomFollower from '$lib/Tools/RandomFollower.svelte';
  import DumpProfile from '$lib/Tools/DumpProfile.svelte';
  import { tools } from '$lib/Tools/tools.js';
  import { onMount } from 'svelte';
  import { goto } from '$app/navigation';
  import Picker from '$lib/Tools/Picker.svelte';
  import Likes from '$lib/Tools/Likes.svelte';
  import root from '$lib/Utility/root.js';
  import Popup from '$lib/Layout/Popup.svelte';
  import HololiveBirthdays from '$lib/Tools/HololiveBirthdays.svelte';
  import SequelCatcher from '$lib/Tools/SequelCatcher/Tool.svelte';

  export let data;

  let tool = data.tool ?? 'default';

  onMount(() => {
    if (tool === 'default') goto(root('/tools'));
  });

  $: suggestion = closest(tool, Object.keys(tools));

  $: if (tool == 'girls') goto(root('/girls'));
</script>

<Picker bind:tool />

{#if !Object.keys(tools).includes(tool)}
  <HeadTitle route="Tools" path="/tools" />

  <Popup>
    <p style="text-align: center;">
      Tool "<a href={root(`/tools/${tool}`)}>{tool}</a>" not found
    </p>

    <blockquote style="margin: 0 0 0 1.5rem;">
      Did you mean "<a
        href={root(`/tools/${tools[suggestion].id}`)}
        on:click={() => (tool = suggestion)}
        style={suggestion === '...' ? 'pointer-events: none; color: inherit;' : ''}
      >
        {suggestion === '...' ? '...' : tools[suggestion].name()}</a
      >"?
    </blockquote>
  </Popup>
{:else}
  <HeadTitle route={tools[tool].name()} path={`/tools?tool=${tool}`} />

  {#if tool === 'activity_history'}
    <ActivityHistory user={data.user} />
  {:else if tool === 'wrapped'}
    <Wrapped user={data.user} />
  {:else if tool === 'discussions'}
    <EpisodeDiscussionCollector />
  {:else if tool === 'birthdays'}
    <CharacterBirthdays />
  {:else if tool === 'sequel_spy'}
    <SequelSpy user={data.user} />
  {:else if tool === 'random_follower'}
    <RandomFollower />
  {:else if tool === 'dump_profile'}
    <DumpProfile />
  {:else if tool === 'likes'}
    <Likes />
  {:else if tool === 'uma_musume_birthdays'}
    <UmaMusumeBirthdays />
  {:else if tool === 'hayai'}
    <Hayai />
  {:else if tool === 'hololive_birthdays'}
    <HololiveBirthdays />
  {:else if tool === 'sequel_catcher'}
    <SequelCatcher user={data.user} />
  {/if}
{/if}
